#include "GuiIngame.hpp"

#include "../Entity/Entity.hpp"
#include "../Render/RenderBase.hpp"
#include "../MapSystem/Map.hpp"
#include "../MapSystem/MapView.hpp"
#include "../Entity/EntityPlayer.hpp"
#include "../Animation/AnimatedSprite.hpp"
#include "../GUI/DialogManager.hpp"

GuiIngame::GuiIngame(Core* core)
{
    m_core = core;
    m_dialogManager = core->dialogManager;
    initGui();
}

GuiIngame::~GuiIngame()
{

}
/**[VIRTUAL]Initialize the GUI set vars etc...**/
void GuiIngame::initGui()
{

}

/**[VIRTUAL] Draw the Screen on the window**/
void GuiIngame::drawScreen()
{
    if(m_dialogManager->isActiveOrWaiting())
    {
        m_core->window.setView(m_core->theWorld->getCamera());
        draw();
        m_core->window.setView(*m_core->guiView);
        drawHUD();
        m_core->window.setView(m_core->window.getDefaultView());
        m_dialogManager->update();
        m_dialogManager->draw(&m_core->window);
        m_core->theWorld->moveCamera(m_core->thePlayer->getPosition(), m_core->thePlayer->getVelocity());
        m_core->theWorld->updateCamera();
    }
    else
    {
        update();
        m_core->window.setView(m_core->theWorld->getCamera());
        draw();
        m_core->window.setView(*m_core->guiView);
        drawHUD();
    }

}

void GuiIngame::update()
{
    Entity::updateEntities(&m_core->window);
    Entity::checkEntitiesCollisions(m_core->theWorld);

    m_core->theWorld->moveCamera(m_core->thePlayer->getPosition(), m_core->thePlayer->getVelocity());
    m_core->theWorld->updateCamera();

    Entity::updateEntitiesPosition();
}

void GuiIngame::draw()
{
    /*m_core->theWorld->animate(0.0166666666);

    Entity::updateEntities(&m_core->window);
    Entity::checkEntitiesCollisions(m_core->theWorld);

    m_core->theWorld->moveCamera(m_core->thePlayer->getRender()->getAnimatedSprite()->getNextX(), m_core->thePlayer->getRender()->getAnimatedSprite()->getNextY());
    m_core->theWorld->updateCamera();

    Entity::updateEntitiesPosition();

    m_core->window.setView(m_core->theWorld->getCamera());

    m_core->theWorld->drawLayer(0, m_core->window);
    m_core->theWorld->drawLayer(1, m_core->window);
    Entity::drawEntities(&m_core->window);
    m_core->theWorld->drawLayer(2,m_core->window);

    m_core->window.setView(*m_core->guiView);*/
    m_core->theWorld->animate();
    m_core->theWorld->drawLayer(0);
    m_core->theWorld->drawLayer(1);
    Entity::drawEntities(&m_core->window);
    m_core->theWorld->drawLayer(2);
}

/**[PRIVATE] Draw the HUD on the screen**/
void GuiIngame::drawHUD()
{
    int healthMax = m_core->thePlayer->getHealthMax();
    int health = m_core->thePlayer->getHealth();
    sf::Texture textureCoeur;
    sf::Texture textureSlotCoeur;
    sf::Sprite sprite;
    if (!textureSlotCoeur.loadFromFile("Ressources/sprites/miniheart_container.png"))
    {
        std::cout << "Impossible de charger le fichier heart_block" << std::endl;
    }
    sprite.setTexture(textureSlotCoeur);
    for(int x = 0; x < healthMax; x++)
    {
        sprite.setPosition(0+20*x,0);
        m_core->window.draw(sprite);
    }

    if (!textureCoeur.loadFromFile("Ressources/sprites/miniheart.png"))
    {
        std::cout << "Impossible de charger le fichier coeur" << std::endl;
    }
    sprite.setTexture(textureCoeur);
    for(int x = 0; x < health; x++)
    {
        sprite.setPosition(sf::Vector2f(0+20*x,0));
        m_core->window.draw(sprite);
    }
}
/**[VIRTUAL] Called when the gui is closed**/
void GuiIngame::onGuiClosed()
{

}
/**[VIRTUAL]Return if the Gui does Pause the game when it's open**/
bool GuiIngame::doesGuiPauseGame()
{
    return false;
}
